package com.qianfeng.es.service.impl;

//
//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖镇楼                  BUG辟易  
//          佛曰:  
//                  写字楼里写字间，写字间里程序员；  
//                  程序人员写程序，又拿程序换酒钱。  
//                  酒醒只在网上坐，酒醉还来网下眠；  
//                  酒醉酒醒日复日，网上网下年复年。  
//                  但愿老死电脑间，不愿鞠躬老板前；  
//                  奔驰宝马贵者趣，公交自行程序员。  
//                  别人笑我忒疯癫，我笑自己命太贱；  
//  


import com.qianfeng.es.service.IndexService;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.cluster.metadata.MappingMetadata;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.xcontent.json.JsonXContent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * Created by Jackiechan on 2022/9/7 21:19
 * @author Jackiechan
 * @since 1.0
 * @version 1.0
 */

@Service
public class IndexServiceIImpl implements IndexService {
    private RestHighLevelClient restHighLevelClient;

    @Autowired
    public void setRestHighLevelClient(RestHighLevelClient restHighLevelClient) {
        this.restHighLevelClient = restHighLevelClient;
    }

    @Override
    public CreateIndexResponse createIndex(String index) throws Exception {
        if (!isExist(index)) {
            //创建index 的请求,参数是要创建的index的名字
            CreateIndexRequest createIndexRequest = new CreateIndexRequest(index);
            //map呢?
            createIndexRequest
                    .settings(Settings.builder().put("number_of_shards",5)
                            .put("number_of_replicas",1))
                    .mapping(JsonXContent.contentBuilder()//设置数据类型是json
                            .startObject()//代表是一个{
                            .startObject("properties")
                            .startObject("corpName").field("type","keyword").endObject()
                            .startObject("fee").field("type","integer").endObject()
                            .startObject("ipAddr").field("type","ip").endObject()
                            .startObject("longCode").field("type","keyword").endObject()
                            .startObject("mobile").field("type","keyword").endObject()
                            .startObject("province").field("type","keyword").endObject()
                            .startObject("operatorId").field("type","integer").endObject()
                            .startObject("replyTotal").field("type","integer").endObject()
                            .startObject("state").field("type","integer").endObject()
                            .startObject("createDate").field("type","date").field("format","yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis").endObject()
                            .startObject("sendDate").field("type","date").field("format","yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis").endObject()
                            .startObject("smsContent").field("type","text").field("analyzer","ik_max_word").endObject()
                            .endObject()
                            .endObject()

                    );

            CreateIndexResponse createIndexResponse = restHighLevelClient.indices()//和index库相关的操作
                    .create(createIndexRequest, RequestOptions.DEFAULT);
            return createIndexResponse;
        }
        return null;
    }

    @Override
    public void getIndex(String index) throws Exception {
        GetIndexResponse response = restHighLevelClient.indices().get(new GetIndexRequest(index), RequestOptions.DEFAULT);
        Map<String, MappingMetadata> mappings = response.getMappings();
        System.err.println(mappings.get(index).getSourceAsMap());
    }

    @Override
    public void deleteIndex(String index) throws Exception {
        if (isExist(index)) {
            AcknowledgedResponse response = restHighLevelClient.indices().delete(new DeleteIndexRequest(index), RequestOptions.DEFAULT);
            System.err.println(response.isAcknowledged());
        }else{
            System.err.println("index不存在");
        }
    }

    @Override
    public boolean isExist(String index) throws Exception {
        //true代表存在
        return restHighLevelClient.indices().exists(new GetIndexRequest(index), RequestOptions.DEFAULT);
    }
}

















